package krt.wid.gslzs.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Spinner;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.krt.webapp.until.BaseUtil;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageGridActivity;
import com.lzy.imagepicker.ui.ImagePreviewDelActivity;
import com.lzy.imagepicker.view.CropImageView;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.rest.Response;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import krt.wid.gslzs.adpater.ImagePickerAdapter;
import krt.wid.gslzs.bean.Adress;
import krt.wid.gslzs.bean.CityEntiy;
import krt.wid.gslzs.bean.Sxh_type;
import krt.wid.gslzs.bean.Work_type;
import krt.wid.gslzs.R;
import krt.wid.gslzs.base.BaseActivity;
import krt.wid.gslzs.util.GlideUtil;
import krt.wid.gslzs.util.ImagePickerUtil;
import krt.wid.gslzs.util.SelectDialog;
import krt.wid.http.MHttp;
import krt.wid.http.MResponseListener;
import krt.wid.http.Result;
import krt.wid.http.upload.MUpload;
import krt.wid.inter.UploadCallBack;
import krt.wid.util.MToast;
import krt.wid.gslzs.util.CountDownButtonHelper;
import krt.wid.gslzs.util.GlideImageLoader;
import krt.wid.gslzs.util.TitleManager;
import krt.wid.util.ParseJsonUtil;

/**
 * @author admin
 * @date 2018/5/8
 * 非企业用户注册
 */

public class Qyyh_RegisterActivity extends BaseActivity implements View.OnClickListener, ImagePickerAdapter.OnRecyclerViewItemClickListener {
    private static final int PHOTO_REQUEST_CAREMA = 1;
    private static final int PHOTO_REQUEST_GALLERY = 2;
    private static final int PHOTO_REQUEST_CUT = 3;
    public static final int IMAGE_ITEM_ADD = -1;
    public static final int REQUEST_CODE_SELECT = 100;
    public static final int REQUEST_CODE_PREVIEW = 101;
    //当前Activity的弱引用，防止内存泄露
    private WeakReference<Activity> context = null;
    FrameLayout leftLayout;
    int biaoshi;
    //    区分商协会用户注册与企业用户注册
// 区分企业名称与商协会名称
    TextView t1;
    TextView t3;
    TextView t5;
    LinearLayout t6;
    TextView uploadtext;

    //注册机构字段
    EditText zc_qymc; //企业名称
    EditText zc_frdb; //法人代表
    EditText zc_xydm; //统一信用码
    TextView zc_shu_adress;//所属地区
    Spinner zc_shu_worke;//所属行业
    EditText zc_qyadress;//企业地址

    //身份证正反面
    ImageView card;
    ImageView card1;
    //注册基础字段
    EditText zc_name; //注册人姓名
    EditText zc_zw; //注册人职务
    EditText zc_phone;//注册人手机号
    EditText zxp_pwd;//注册人密码
    EditText zc_yzm;//注册验证码
    //获取验证码


    //注册按钮
    Button reall_name_confirm;

    String[] aderssName = new String[]{};
    String[] type = new String[]{};

    private ArrayAdapter<String> adapter;

    //照片区分标识符
    int photo_qufen;

    //获取照片上传路径
    String path;
    String path2;
    //获取验证码
    Button get_code;

    //字段为标识
    String adress;
    //商协会类型 行业类别
    String sxtype;
    String worktype;

    //获取图片地址url
    String url1 = "";
    String url2 = "";
    String url3 = "";

    int photo_biaoshi = 0;


    //多张图片上传
    private ImagePickerAdapter imgadapter;
    private ArrayList<ImageItem> selImageList; //当前选择的所有图片
    private int maxImgCount = 1;               //允许选择图片最大数


    private List<CityEntiy> mList;
    private List<String> options1Items = new ArrayList<>();
    private ArrayList<ArrayList<String>> options2Items = new ArrayList<>();
    private ArrayList<ArrayList<ArrayList<String>>> options3Items = new ArrayList<>();
    private OptionsPickerView pickView;

    @Override
    public int bindLayout() {
        return R.layout.qyyh_register;
    }

    @Override
    public void initView() {

        initWheelData();
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        selImageList = new ArrayList<>();
        imgadapter = new ImagePickerAdapter(this, selImageList, maxImgCount);
        imgadapter.setOnItemClickListener(this);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 1));
        recyclerView.setHasFixedSize(true);
        recyclerView.setAdapter(imgadapter);


        //访问相册
        initImagePicker();
        //新页面接收数据
        Bundle bundle = this.getIntent().getExtras();
        //接收name值
        biaoshi = bundle.getInt("biaoshi");
        Log.w("biaoshi", String.valueOf(biaoshi));
        //设置标题栏
        TitleManager titleManager = new TitleManager(this);
        titleManager.hideDivider();
        t1 = findViewById(R.id.capable01);
        t3 = findViewById(R.id.capable03);
        t5 = findViewById(R.id.capable05);
        t6 = findViewById(R.id.capable06);
        uploadtext = findViewById(R.id.upload_text);
        if (biaoshi == 1) {
            titleManager.setCenterText("非公企业用户注册", ContextCompat.getColor(this, R.color.white), 18);
            t1.setText("企业名称 :");
            t3.setText("统一信用代码 :");
            t5.setText("所属行业  :");
            uploadtext.setText("上传营业执照照片");
        } else {
            titleManager.setCenterText("商协会用户注册", ContextCompat.getColor(this, R.color.white), 18);
            t1.setText("商协会名称 :");
            t3.setText("组织机构代码 :");
            t5.setText("商协会类型 :");
            t6.setVisibility(View.GONE);
            uploadtext.setText("上传组织机构代码证照片");

        }

        leftLayout = findViewById(R.id.left_layout);
        leftLayout.setOnClickListener(this);

        //初始化注册信息
//        <!--注册机构字段-->
        zc_qymc = findViewById(R.id.zc_qymc);
        zc_frdb = findViewById(R.id.zc_frdb);
        zc_xydm = findViewById(R.id.zc_xydm);
        zc_shu_adress = findViewById(R.id.zc_shu_adress);
        zc_shu_adress.setOnClickListener(this);
        zc_shu_worke = findViewById(R.id.zc_shu_worke);
        zc_qyadress = findViewById(R.id.zc_qyadress);

        //用户基础信息初始化
        zc_name = findViewById(R.id.zc_name);
        zc_zw = findViewById(R.id.zc_zw);
        zc_phone = findViewById(R.id.zc_phone);
        zxp_pwd = findViewById(R.id.zxp_pwd);
        zc_yzm = findViewById(R.id.zc_yzm);

        //注册 按钮
        reall_name_confirm = findViewById(R.id.reall_name_confirm);
        //监听事件
        reall_name_confirm.setOnClickListener(this);

        //获取验证码
        get_code = findViewById(R.id.get_code);
        get_code.setOnClickListener(this);
        //获取地区
        String url = "http://111.75.240.74:13381/non_public_economy/api/regionList";
        MHttp.<Result<List<Adress>>>create(this).setUrl(url)
                .setMethod(RequestMethod.GET)
                .execute(new MResponseListener<Result<List<Adress>>>() {
                    @Override
                    public void onSuccess(int what, Result<List<Adress>> listResult) {
                        if (listResult.isSuccess()) {
                            //    MToast.showToast(Qyyh_RegisterActivity.this, String.valueOf(listResult.getData().size()));
                            aderssName = new String[listResult.getData().size()];
                            for (int i = 0; i < listResult.getData().size(); i++) {
                                aderssName[i] = listResult.getData().get(i).getName();
                            }
                            adapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_spinner_item, aderssName);
                            //设置样式
                            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        }

                    }

                    @Override
                    public void onFailed(int what, Response<Result<List<Adress>>> response) {

                    }
                });
        String url1 = "http://111.75.240.74:13381/non_public_economy/api/dicList";
        //获取商协会类型
        if (biaoshi == 1) {
            MHttp.<Result<List<Work_type>>>create(this).setUrl(url1)
                    .setMethod(RequestMethod.GET)
                    .addParams("dictionaryType", "industry_type")
                    .execute(new MResponseListener<Result<List<Work_type>>>() {
                        @Override
                        public void onSuccess(int what, Result<List<Work_type>> listResult) {
                            type = new String[listResult.getData().size()];
                            for (int i = 0; i < listResult.getData().size(); i++) {
                                type[i] = listResult.getData().get(i).getName();
                            }
                            adapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_spinner_item, type);
                            //设置样式
                            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                            zc_shu_worke.setAdapter(adapter);

                        }

                        @Override
                        public void onFailed(int what, Response<Result<List<Work_type>>> response) {

                        }
                    });
        } else {
            MHttp.<Result<List<Sxh_type>>>create(this).setUrl(url1)
                    .setMethod(RequestMethod.GET)
                    .addParams("dictionaryType", "org_type")
                    .execute(new MResponseListener<Result<List<Sxh_type>>>() {
                        @Override
                        public void onSuccess(int what, Result<List<Sxh_type>> listResult) {
                            type = new String[listResult.getData().size()];
                            for (int i = 0; i < listResult.getData().size(); i++) {
                                type[i] = listResult.getData().get(i).getName();
                            }
                            adapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_spinner_item, type);
                            //设置样式
                            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                            zc_shu_worke.setAdapter(adapter);
                        }

                        @Override
                        public void onFailed(int what, Response<Result<List<Sxh_type>>> response) {

                        }
                    });

        }

        //身份证单张上传
        card = findViewById(R.id.card);
        card1 = findViewById(R.id.card1);
        card = findViewById(R.id.card);
        card1 = findViewById(R.id.card1);
        card.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                List<String> names = new ArrayList<>();
                names.add("拍照");
                names.add("相册");
                showDialog(new SelectDialog.SelectDialogListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        ImagePicker.getInstance().setMultiMode(false);
                        Intent intent = new Intent(Qyyh_RegisterActivity.this, ImageGridActivity.class);
                        switch (position) {
                            case 0: // 直接调起相机
                                /**
                                 * 0.4.7 目前直接调起相机不支持裁剪，如果开启裁剪后不会返回图片，请注意，后续版本会解决
                                 *
                                 * 但是当前直接依赖的版本已经解决，考虑到版本改动很少，所以这次没有上传到远程仓库
                                 *
                                 * 如果实在有所需要，请直接下载源码引用。
                                 */
                                //打开选择,本次允许选择的数量

                                intent.putExtra(ImageGridActivity.EXTRAS_TAKE_PICKERS, true); // 是否是直接打开相机
                                startActivityForResult(intent, REQUEST_CODE_SELECT);
                                break;
                            case 1:
                                //打开选择,本次允许选择的数量
                                Intent intent1 = new Intent(Qyyh_RegisterActivity.this, ImageGridActivity.class);
                                /* 如果需要进入选择的时候显示已经选中的图片，
                                 * 详情请查看ImagePickerActivity
                                 * */
//                                intent1.putExtra(ImageGridActivity.EXTRAS_IMAGES,images);
                                startActivityForResult(intent1, REQUEST_CODE_SELECT);
                                break;
                            default:
                                break;
                        }

                    }
                }, names);
                photo_biaoshi = 1;

            }
        });
        card1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                List<String> names = new ArrayList<>();
                names.add("拍照");
                names.add("相册");
                showDialog(new SelectDialog.SelectDialogListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        ImagePicker.getInstance().setMultiMode(false);
                        Intent intent = new Intent(Qyyh_RegisterActivity.this, ImageGridActivity.class);
                        switch (position) {
                            case 0: // 直接调起相机
                                /**
                                 * 0.4.7 目前直接调起相机不支持裁剪，如果开启裁剪后不会返回图片，请注意，后续版本会解决
                                 *
                                 * 但是当前直接依赖的版本已经解决，考虑到版本改动很少，所以这次没有上传到远程仓库
                                 *
                                 * 如果实在有所需要，请直接下载源码引用。
                                 */
                                //打开选择,本次允许选择的数量

                                intent.putExtra(ImageGridActivity.EXTRAS_TAKE_PICKERS, true); // 是否是直接打开相机
                                startActivityForResult(intent, REQUEST_CODE_SELECT);
                                break;
                            case 1:
                                //打开选择,本次允许选择的数量
                                Intent intent1 = new Intent(Qyyh_RegisterActivity.this, ImageGridActivity.class);
                                /* 如果需要进入选择的时候显示已经选中的图片，
                                 * 详情请查看ImagePickerActivity
                                 * */
//                                intent1.putExtra(ImageGridActivity.EXTRAS_IMAGES,images);
                                startActivityForResult(intent1, REQUEST_CODE_SELECT);
                                break;
                            default:
                                break;
                        }

                    }
                }, names);
                photo_biaoshi = 2;

            }
        });


    }

    @Override
    public void loadData() {

    }

    private void initWheelData() {
        mList = ParseJsonUtil.getBeanList(BaseUtil.getJson(this, "city.json"), CityEntiy.class);
        for (int i = 0; i < mList.size(); i++) {//遍历省份
            ArrayList<String> CityList = new ArrayList<>();//该省的城市列表（第二级）
            ArrayList<ArrayList<String>> Province_AreaList = new ArrayList<>();//该省的所有地区列表（第三极）
            options1Items.add(mList.get(i).getName());
            for (int c = 0; c < mList.get(i).getCity().size(); c++) {//遍历该省份的所有城市
                String CityName = mList.get(i).getCity().get(c).getName();
                CityList.add(CityName);//添加城市

                ArrayList<String> City_AreaList = new ArrayList<>();//该城市的所有地区列表

                //如果无地区数据，建议添加空字符串，防止数据为null 导致三个选项长度不匹配造成崩溃
                if (mList.get(i).getCity().get(c).getName() == null
                        || mList.get(i).getCity().get(c).getCounty().size() == 0) {
                    City_AreaList.add("");
                } else {

                    for (int d = 0; d < mList.get(i).getCity().get(c).getCounty().size(); d++) {//该城市对应地区所有数据
                        String AreaName = mList.get(i).getCity().get(c).getCounty().get(d).getName();
                        City_AreaList.add(AreaName);//添加该城市所有地区数据
                    }
                }
                Province_AreaList.add(City_AreaList);//添加该省所有地区数据
            }

            /**
             * 添加城市数据
             */
            options2Items.add(CityList);

            /**
             * 添加地区数据
             */
            options3Items.add(Province_AreaList);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.left_layout:
                finish();
                overridePendingTransition(R.anim.in2, R.anim.out2);
                break;

            case R.id.get_code:
                if (!zc_phone.getText().toString().equals("") && zc_phone.getText().toString().length() == 11) {
                    CountDownButtonHelper helper = new CountDownButtonHelper(get_code,
                            "发送验证码", 60, 1);
                    helper.setOnFinishListener(new CountDownButtonHelper.OnFinishListener() {

                        @Override
                        public void finish() {
                        }
                    });
                    helper.start();

                    String url = "http://111.75.240.74:13381/non_public_economy/api/getVerifyCode";
                    MHttp.<Result>create(Qyyh_RegisterActivity.this).setUrl(url)
                            .addParams("phone", zc_phone.getText().toString())
                            .execute(new MResponseListener<Result>() {

                                @Override
                                public void onSuccess(int what, Result result) {
                                    //   MToast.showToast(Qyyh_RegisterActivity.this,result.getMsg());

                                }

                                @Override
                                public void onFailed(int what, Response<Result> response) {

                                }
                            });

                } else {
                    MToast.showToast(Qyyh_RegisterActivity.this, "输入正确的手机号");
                }
                break;

            //注册按钮
            case R.id.reall_name_confirm:
                //注册机构字段
                //  注册机构字段从上往下字段分别用p1-p6
                String p1 = zc_qymc.getText().toString();
                if (p1.equals("") && biaoshi == 1) {
                    MToast.showToast(Qyyh_RegisterActivity.this, "企业名称不能为空");
                    break;
                }
                if (p1.equals("") && biaoshi == 2) {
                    MToast.showToast(Qyyh_RegisterActivity.this, "商协会名称不能为空");
                    break;
                }
                String p2 = zc_frdb.getText().toString();
                if (p2.equals("")) {
                    MToast.showToast(Qyyh_RegisterActivity.this, "法人代表不能为空");
                    break;
                }
                String p3 = zc_xydm.getText().toString();

                if (p3.equals("") && biaoshi == 1) {
                    MToast.showToast(Qyyh_RegisterActivity.this, "统一信用码不能为空");
                    break;
                }
                if (p3.equals("") && biaoshi == 2) {
                    MToast.showToast(Qyyh_RegisterActivity.this, "组织机构代码不能为空");
                    break;
                }


                String p4 = zc_shu_adress.getText().toString();
                if (p4.equals("")) {
                    MToast.showToast(Qyyh_RegisterActivity.this, "所属地区不能为空");
                    break;
                }
                //所属行业

                String p5 = "公务员";
                //企业单位需要字段
                String p6 = zc_qyadress.getText().toString();
                if (p6.equals("") && biaoshi == 1) {
                    MToast.showToast(Qyyh_RegisterActivity.this, "企业地址不能为空");
                    break;
                }
                //照片为公共变量url1-url5

                //注册基础字段
                String c1 = zc_name.getText().toString();
                String c2 = zc_zw.getText().toString();
                String c3 = zc_phone.getText().toString();
                String c4 = zxp_pwd.getText().toString();
                String c5 = zc_yzm.getText().toString();
                if (c1.equals("")) {
                    MToast.showToast(Qyyh_RegisterActivity.this, "注册人姓名不能为空");
                    break;
                }
                if (c2.equals("")) {
                    MToast.showToast(Qyyh_RegisterActivity.this, "注册人职务不能为空");
                    break;
                }
                if (c3.equals("") ||c3.length() != 11) {
                    MToast.showToast(Qyyh_RegisterActivity.this, "请输入正确的手机号");
                    break;
                }
                if (c4.equals("")) {
                    MToast.showToast(Qyyh_RegisterActivity.this, "密码不能为空");
                    break;
                }
                if (c5.equals("")) {
                    MToast.showToast(Qyyh_RegisterActivity.this, "验证码不能为空");
                    break;
                }
                if (url1 == "") {
                    MToast.showToast(Qyyh_RegisterActivity.this, "请上传身份正面照片");
                    break;
                }
                if (url2 == "") {
                    MToast.showToast(Qyyh_RegisterActivity.this, "请上传身份证反面照片");
                    break;
                }
                if (url3 == "" && biaoshi == 1) {
                    MToast.showToast(Qyyh_RegisterActivity.this, "请上传营业执照照片");
                    break;
                }
                if (url3 == "" && biaoshi == 2) {
                    MToast.showToast(Qyyh_RegisterActivity.this, "请上传组织机构代码照片");
                    break;
                }

                if (biaoshi == 1) {
                    String url = "http://111.75.240.74:13381/non_public_economy/api/enterprise/register";
                    MHttp.<Result>create(this).setUrl(url)
                            .addParams("ename", p1)
                            .addParams("legalName", p2)
                            .addParams("uscc", p3)
                            .addParams("regionName", p4)
                            .addParams("region", "2")
                            .addParams("industry", p5)
                            .addParams("industryName", "org_type")
                            .addParams("address", p4)
                            .addParams("businessLicense", url3)//多张照片
                            .addParams("linkIdentityNum1", url1)
                            .addParams("linkIdentityNum2", url2)
                            .addParams("linkMan", c1)
                            .addParams("linkDuties", c2)
                            .addParams("linkPhone", c3)
                            .addParams("password", c4)
                            .execute(new MResponseListener<Result>() {

                                @Override

                                public void onSuccess(int what, Result result) {
                                    if (result.isSuccess()) {
                                        MToast.showToast(Qyyh_RegisterActivity.this, result.getMsg());
                                        finish();
                                        overridePendingTransition(R.anim.in2, R.anim.out2);
                                    } else {
                                        MToast.showToast(Qyyh_RegisterActivity.this, result.getMsg());
                                    }

                                }

                                @Override
                                public void onFailed(int what, Response<Result> response) {

                                }
                            });
                }
                if (biaoshi == 2) {
                    String url = "http://111.75.240.74:13381/non_public_economy/api/coc/Register";
                    MHttp.<Result>create(this).setUrl(url)
                            .addParams("cname", p1)
                            .addParams("legalName", p2)
                            .addParams("organizationCode", p3)
                            .addParams("region", p4)
                            .addParams("regionName", "2")
                            .addParams("orgType", p5)
                            .addParams("orgtypeName", "org_type")
                            .addParams("registerImage", url3)//多张照片
                            .addParams("cardImage1", url1)
                            .addParams("cardImage2", url2)
                            .addParams("registerName", c1)
                            .addParams("registerJob", c2)
                            .addParams("registerPhone", c3)
                            .addParams("password", c4)

                            .execute(new MResponseListener<Result>() {
                                @Override
                                public void onSuccess(int what, Result result) {
                                    if (result.isSuccess()) {
                                        MToast.showToast(Qyyh_RegisterActivity.this, result.getMsg());
                                        finish();
                                        overridePendingTransition(R.anim.in2, R.anim.out2);
                                    } else {
                                        MToast.showToast(Qyyh_RegisterActivity.this, result.getMsg());
                                    }

                                }

                                @Override
                                public void onFailed(int what, Response<Result> response) {

                                }
                            });
                }

                break;
            case R.id.zc_shu_adress:
                if (options1Items.isEmpty() || options2Items.isEmpty() || options3Items.isEmpty())
                    return;
                pickView = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        //返回的分别是三个级别的选中位置
                        String tx = options1Items.get(options1) +
                                options2Items.get(options1).get(options2) +
                                options3Items.get(options1).get(options2).get(options3);
                        zc_shu_adress.setText(tx);
//                        code = mList.get(options1).getCity().get(options2).getCounty().get(options3).getCode();
                    }
                }).setTitleText("城市选择")
                        .setDividerColor(Color.BLACK)
                        .setTextColorCenter(ContextCompat.getColor(this, R.color.color_1b9ef6)) //设置选中项文字颜色
                        .setContentTextSize(18)
                        .build();
                pickView.setPicker(options1Items, options2Items, options3Items);//三级选择器
                pickView.show();
                break;

            default:
                break;

        }
    }

    /**
     * 获取当前Activity
     *
     * @return
     */
    protected Activity getContext() {
        if (null != context) {
            return context.get();
        } else {
            return null;
        }
    }


    private void initImagePicker() {
        ImagePicker imagePicker = ImagePicker.getInstance();
        imagePicker.setImageLoader(new GlideImageLoader());   //设置图片加载器
        imagePicker.setShowCamera(true);
        //显示拍照按钮
        if (photo_biaoshi == 0) {
            imagePicker.setMultiMode(true);
        } else {
            imagePicker.setMultiMode(false);
        }
        imagePicker.setCrop(false);//允许裁剪（单选才有效）
        imagePicker.setSaveRectangle(true);
        //是否按矩形区域保存
        if (photo_biaoshi == 0) {
            imagePicker.setSelectLimit(maxImgCount);
        } else {
            imagePicker.setSelectLimit(1);
        }

        //选中数量限制
        imagePicker.setStyle(CropImageView.Style.RECTANGLE);  //裁剪框的形状
        imagePicker.setFocusWidth(800);                       //裁剪框的宽度。单位像素（圆形自动取宽高最小值）
        imagePicker.setFocusHeight(800);                      //裁剪框的高度。单位像素（圆形自动取宽高最小值）
        imagePicker.setOutPutX(1000);                         //保存文件的宽度。单位像素
        imagePicker.setOutPutY(1000);                         //保存文件的高度。单位像素

    }

    @Override
    public void onItemClick(View view, int position) {
        switch (position) {
            case IMAGE_ITEM_ADD:
                List<String> names = new ArrayList<>();
                names.add("拍照");
                names.add("相册");
                showDialog(new SelectDialog.SelectDialogListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        photo_biaoshi = 0;
                        ImagePicker.getInstance().setMultiMode(true);
                        ImagePicker.getInstance().setSelectLimit(maxImgCount - selImageList.size());
                        Intent intent1 = new Intent(Qyyh_RegisterActivity.this, ImageGridActivity.class);
                        if (position == 0) {
                            intent1.putExtra(ImageGridActivity.EXTRAS_TAKE_PICKERS, true); // 是否是直接打开相机
                        }
                        startActivityForResult(intent1, REQUEST_CODE_SELECT);
                    }
                }, names);

                break;
            default:
                //打开预览
                Intent intentPreview = new Intent(this, ImagePreviewDelActivity.class);
                intentPreview.putExtra(ImagePicker.EXTRA_IMAGE_ITEMS, (ArrayList<ImageItem>) imgadapter.getImages());
                intentPreview.putExtra(ImagePicker.EXTRA_SELECTED_IMAGE_POSITION, position);
                intentPreview.putExtra(ImagePicker.EXTRA_FROM_ITEMS, true);
                startActivityForResult(intentPreview, REQUEST_CODE_PREVIEW);
                break;
        }
    }

    private SelectDialog showDialog(SelectDialog.SelectDialogListener listener, List<String> names) {
        SelectDialog dialog = new SelectDialog(this, R.style
                .transparentFrameWindowStyle,
                listener, names);
        if (!this.isFinishing()) {
            dialog.show();
        }
        return dialog;
    }

    ArrayList<ImageItem> images = null;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {
            //添加图片返回
            if (data != null && requestCode == REQUEST_CODE_SELECT) {
                images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                if (images != null && photo_biaoshi == 1) {
                    path = images.get(0).path;
//                    GlideUtil.load(Qyyh_RegisterActivity.this, path, card);
                    Log.w("photourl...", path);
                    List list = new ArrayList();
                    list.add(path);
                    upload2(list);
                } else if (images != null && photo_biaoshi == 2) {
                    path2 = images.get(0).path;
                    Log.w("photourl00...", path2);
                    List list = new ArrayList();
                    list.add(path2);
                    upload2(list);
                } else {
                    selImageList.addAll(images);
                    imgadapter.setImages(selImageList);
                    List<String> list = new ArrayList<>();
                    for (ImageItem item : selImageList) {
                        list.add(item.path);
                    }
                    upload2(list);
                }
            }
        } else if (resultCode == ImagePicker.RESULT_CODE_BACK) {
            //预览图片返回
            if (data != null && requestCode == REQUEST_CODE_PREVIEW) {
                images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_IMAGE_ITEMS);
                if (images != null) {
                    selImageList.clear();
                    selImageList.addAll(images);
                    imgadapter.setImages(selImageList);
                }
            }
        }

    }
    //多张上传

    private void upload(List<String> list) {

        MUpload.newBuilder(Qyyh_RegisterActivity.this)
                .setUrl("http://111.75.240.74:13381/non_public_economy/api/uploadList")
                .setPaths("file", list)
                .setNeedCompress(true)
                .execute(new UploadCallBack() {
                    @Override
                    public void onSuccess(String json) {
//                        Log.w("zzzzz",ParseJsonUtil.getStringByKey(json,"realUrl"))
                        Log.w("上sccg", json);

                    }
                });

    }

    //单张上传
    private void upload2(List paths) {
        MUpload.newBuilder(this).setUrl("http://111.75.240.74:13381/non_public_economy/api/upload")
                .setPaths("file", paths)
                .setNeedCompress(true)
                .execute(new UploadCallBack() {
                    @Override
                    public void onSuccess(String json) {
                        if (photo_biaoshi == 1) {
                            url1 = ParseJsonUtil.getStringByKey(json, "url");
                            String p1 = ParseJsonUtil.getStringByKey(json, "realUrl");
                            GlideUtil.loadWithPlaceHolder(Qyyh_RegisterActivity.this, p1, R.mipmap.add_card, card);
                            Log.w("url..", url1);
                        } else if (photo_biaoshi == 2) {
                            url2 = ParseJsonUtil.getStringByKey(json, "url");
                            String p2 = ParseJsonUtil.getStringByKey(json, "realUrl");
                            GlideUtil.loadWithPlaceHolder(Qyyh_RegisterActivity.this, p2, R.mipmap.add_card, card1);
                            Log.w("url2..", url2);
                        } else {
                            url3 = ParseJsonUtil.getStringByKey(json, "url");
                            //     String p3=ParseJsonUtil.getStringByKey(json, "realUrl");
                            //     GlideUtil.loadWithPlaceHolder(Qyyh_RegisterActivity.this, p3, R.mipmap.add_card, card1);
                            Log.w("url3..", url3);
                        }
                    }

                });
    }
}
